package com.huobao.yunying.activitys;

import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.huobao.yunying.base.HuobaoBaseActivity;
import com.huobao.yunying.entity.DetailCart_data;
import com.huobao.yunying.entity.DetailGoodsData;
import com.huobao.yunying.entity.DetailStoreData;
import com.huobao.yunying.entity.Detail_images;
import com.huobao.yunying.entity.Detail_specs;
import com.huobao.yunying.entity.Detailprice_intervals;
import com.huobao.yunying.entity.Detailprops;
import com.huobao.yunying.goodsdetail_page.GoodsDetailPage;
import com.huobao.yunying.goodsdetail_page.TuwenPage;
import com.huobao.yunying.https.Http;
import com.huobao.yunying.https.HttpConst;
import com.huobao.yunying.https.HttpListener;
import com.huobao.yunying.https.HttpUtil;
import com.huobao.yunying.utils.SkipUtils;
import com.huobao.zhangying.R;
import com.mcoy.snapscrollview.McoySnapPageLayout;

import org.simple.eventbus.EventBus;
import org.simple.eventbus.Subscriber;
import org.xutils.view.annotation.ViewInject;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class Act_Detail extends HuobaoBaseActivity implements OnClickListener {

	@ViewInject(R.id.gouwuche_view)
	private View gouwuche_view;
	@ViewInject(R.id.tv_gouwuche_num)
	private TextView tv_gouwuche_num;

	// 商品id
	private String goodsId;
	// 商品数据
	private DetailGoodsData goodsData;
	// 店铺数据
	private DetailStoreData storeData;
	// 购物车
	private DetailCart_data cartData;

	@ViewInject(R.id.tv_save)
	private TextView tv_save;

	// 加入进货车按钮
	@ViewInject(R.id.tv_jinhuozhe)
	private TextView tv_jinhuozhe;
	// 联系按钮
	@ViewInject(R.id.tv_tel)
	private TextView tv_tel;
	// 立即订购按钮
	@ViewInject(R.id.tv_dinggou)
	private TextView tv_dinggou;


	private String default_img;

	@ViewInject(R.id.flipLayout)
	private McoySnapPageLayout pagelayout;

	private GoodsDetailPage goodsDetailPage;
	private TuwenPage tuwenPage;

	@ViewInject(R.id.tv_kefu)
	private TextView tv_kefu;

	@Override
	protected int getContentView() {
		return R.layout.layout_detail_scrollpage;
	}


	@Override
	protected String getTitleText() {
		return null;
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		EventBus.getDefault().unregister(this);
	}

	@Override
	protected void init() {
		EventBus.getDefault().register(this);
		goodsId = getIntent().getStringExtra("id");
		default_img = getIntent().getStringExtra("image");
		if (goodsId == null) {
			Toast.makeText(context, "传入goodsid", Toast.LENGTH_SHORT).show();
			finish();
			return;
		}
		tv_tel.setOnClickListener(this);
		tv_save.setOnClickListener(this);
		gouwuche_view.setOnClickListener(this);
		tv_jinhuozhe.setOnClickListener(this);
		tv_dinggou.setOnClickListener(this);
		tv_kefu.setOnClickListener(this);
		getDetail();
	}

	private void getDetail() {
		Map<String, String> map = HttpUtil.getParams(HttpConst.FRIST_KEY, "goods", "id", goodsId);
		Http.getInstant().sendRequest(context, map, new HttpListener() {

			@Override
			public void onHttpSuccess(String result) {
				JSONObject jsonObject = JSON.parseObject(result);
				jsonObject = jsonObject.getJSONObject("retval");
				JSONObject good_data = jsonObject.getJSONObject("good_data");
				goodsData = JSON.parseObject(good_data.toString(), DetailGoodsData.class);
				goodsData.price_intervals = JSON.parseArray(good_data.getString("price_intervals"),
						Detailprice_intervals.class);
				goodsData._specs = JSON.parseArray(good_data.getString("_specs"), Detail_specs.class);
				goodsData._images = JSON.parseArray(good_data.getString("_images"), Detail_images.class);
				goodsData.props = JSON.parseArray(good_data.getString("props"), Detailprops.class);
				goodsData._spec_arr = JSON.parseArray(good_data.getString("_spec_arr"), String.class);

				List<List<List<String>>> lists = new ArrayList<List<List<String>>>();
				JSONArray _spec_json = good_data.getJSONArray("_spec_json");
				for (int i = 0; i < _spec_json.size(); i++) {
					JSONArray array = _spec_json.getJSONArray(i);
					List<List<String>> l = new ArrayList<List<String>>();
					for (int j = 0; j < array.size(); j++) {
						JSONArray jsonArray = array.getJSONArray(j);
						List<String> list = JSON.parseArray(jsonArray.toString(), String.class);
						l.add(list);
					}
					lists.add(l);
				}
				goodsData._spec_json = lists;

				JSONObject store_data = jsonObject.getJSONObject("store_data");
				storeData = JSON.parseObject(store_data.toString(), DetailStoreData.class);

				JSONObject cart_data = jsonObject.getJSONObject("cart_data");
				cartData = JSON.parseObject(cart_data.toString(), DetailCart_data.class);

				if(goodsId.equals("2")){
					tv_jinhuozhe.setEnabled(false);
					tv_dinggou.setEnabled(false);
					tv_jinhuozhe.setTextColor(Color.WHITE);
					tv_dinggou.setTextColor(Color.WHITE);
				}
				if (goodsData.genre.equals("3")) {
					tv_jinhuozhe.setVisibility(View.INVISIBLE);
					tv_dinggou.setText("咨询代理");
				}
				mAbTitleBar.setTitleText(goodsData.goods_name);
				tv_gouwuche_num.setText(cartData.cart_num);
				int top = goodsData.is_collect == 0 ? R.mipmap.icon7 : R.mipmap.icon7_press;
				tv_save.setCompoundDrawablesWithIntrinsicBounds(0,top,0,0);
				goodsDetailPage = new GoodsDetailPage(Act_Detail.this, getLayoutInflater().inflate(
						R.layout.layout_detail_page, null), mAbTitleBar, goodsData, storeData, cartData);
				tuwenPage = new TuwenPage(Act_Detail.this, getLayoutInflater().inflate(
						R.layout.layout_tuwenxiangqing_page, null), mAbTitleBar, goodsData, storeData, cartData);
				pagelayout.setSnapPages(goodsDetailPage, tuwenPage);
			}

			@Override
			public void onHttpError(String error) {
				finish();
			}
		});
	}


	private void onTelClick() {
		if (storeData.tel == null)
			return;
		Uri uri = Uri.parse("tel:" + storeData.tel);
		Intent intent = new Intent(Intent.ACTION_DIAL, uri);
		startActivity(intent);
	}


	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.tv_tuwen_detail:// 图文详情
				Intent intent = new Intent(this, Act_Tuwenxiangqing.class);
				intent.putExtra("data", goodsData);
				intent.putExtra("storeData", storeData);
				intent.putExtra("cartData", cartData);
				intent.putExtra("id", goodsData.goods_id);
				intent.putExtra("description", goodsData.description);
//			intent.putParcelableArrayListExtra("images", (ArrayList<? extends Parcelable>) goodsData._images);
//			intent.putParcelableArrayListExtra("props", (ArrayList<? extends Parcelable>) goodsData.props);
				intent.putExtra("images", (Serializable) goodsData._images);
				intent.putExtra("props", (Serializable) goodsData.props);
				startActivity(intent);
				// overridePendingTransition(R.anim.push_up_in,
				// R.anim.push_up_out);
				break;
			case R.id.llyt_spec:// 商品属性
				Intent in = new Intent(this, Act_Select_GuigeAndNum.class);
				in.putExtra("data", goodsData);
				in.putExtra("gouwuche", cartData.cart_num);
				startActivity(in);
				break;
			case R.id.tv_tel:
				onTelClick();
				break;
			case R.id.tv_save:
				save();
				break;
			case R.id.gouwuche_view:// 跳转到购物车界面
				intent = new Intent(this, Act_Main.class);
				intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
				EventBus.getDefault().post("2", "ShowGouwuche");
				startActivity(intent);
				finish();
				break;
			case R.id.tv_jinhuozhe://加入购物车按钮
				Intent in1 = new Intent(this, Act_Select_GuigeAndNum.class);
				in1.putExtra("data", goodsData);
				in1.putExtra("gouwuche", cartData.cart_num);
				startActivity(in1);
				break;
			case R.id.tv_dinggou:
				if (goodsData.genre.equals("3")) {
					intent = new Intent(this, Act_Zixuan_Daili.class);
					intent.putExtra("id", goodsId);
					intent.putExtra("sid", storeData.store_id);
					startActivity(intent);
				} else {
					in1 = new Intent(this, Act_Select_GuigeAndNum.class);
					in1.putExtra("data", goodsData);
					in1.putExtra("gouwuche", cartData.cart_num);
					in1.putExtra("isQuit",true);
					startActivity(in1);
				}
				break;
			case R.id.btn_go_store:
			case R.id.btn_all_progect:
				break;
			case R.id.tv_kefu:
				Bundle bundle = new Bundle();
				bundle.putSerializable("data", goodsData);
				SkipUtils.toChat(this, bundle);
				break;
		}
	}

	@Subscriber(tag = "editGouwucheSuccess")
	public void editGouwucheSuccess(String s) {
		getDetail();
	}

	private void save() {
		Map<String, String> map = HttpUtil.getParams(HttpConst.FRIST_KEY, "goods", HttpConst.SECOND_KEY, "goodscollect",
				"id", goodsData.goods_id);
		Http.getInstant().sendRequest(context, map, new HttpListener() {

			@Override
			public void onHttpSuccess(String result) {
				JSONObject jsonObject = JSON.parseObject(result);
				Toast.makeText(context, jsonObject.getString("retval"), Toast.LENGTH_SHORT).show();
				tv_save.setCompoundDrawablesWithIntrinsicBounds(0, R.mipmap.icon7_press, 0, 0);
			}

			@Override
			public void onHttpError(String error) {

			}
		});
	}

}
